//
//  LZTEmoticonPageView.swift
//  LZTWeibo
//
//  Created by Yin jianxun on 16/8/18.
//  Copyright © 2016年 YinJianxun. All rights reserved.
//

import UIKit

//cell可重用标识符
private let LZTEmoticonPageViewCellId = "LZTEmoticonPageViewCell"

class LZTEmoticonPageView: UICollectionView {

    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        
        super.init(frame: frame, collectionViewLayout: UICollectionViewFlowLayout())
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //设置视图
    func setupUI() {
        
        //backgroundColor = randomColor()
        
        //注册cell
        registerClass(LZTEmoticonPageViewCell.self, forCellWithReuseIdentifier: LZTEmoticonPageViewCellId)
        
        //设置代理
        dataSource = self
    }
    
    override func layoutSubviews() {
        
        super.layoutSubviews()
        
        //设置layout
        let layout = self.collectionViewLayout as! UICollectionViewFlowLayout
        //设置itemSize
        layout.itemSize = self.frame.size
        
        //设置垂直和水平间距
        layout.minimumLineSpacing = 0
        layout.minimumInteritemSpacing = 0
        //滚动方向
        layout.scrollDirection = .Horizontal
    }
    
}

extension LZTEmoticonPageView: UICollectionViewDataSource{
    
    func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
        return LZTEmoticonTools.sharedTools.allEmoticons.count
    }
    
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return LZTEmoticonTools.sharedTools.allEmoticons[section].count
    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(LZTEmoticonPageViewCellId, forIndexPath: indexPath) as! LZTEmoticonPageViewCell
        cell.indexPath = indexPath
        
        //表情的一位数组 赋值
        cell.emoticons = LZTEmoticonTools.sharedTools.allEmoticons[indexPath.section][indexPath.item]
        
        //cell的颜色设定
        cell.backgroundColor = self.backgroundColor
        
        return cell
        
    }
    
}
